{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c2b7da21-f761-4b32-9b9e-f8985e2f8dc9",
   "metadata": {},
   "source": [
    "Chapter 7\n",
    "# 习题答案\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7fd0dce-2785-492d-8c71-40feddea5500",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 习题都没有固定答案，请大家多多思考；如果有好的逻辑，请邮件 jiang.visualize.ml@gmail.com。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16f6a4b0-04f3-457a-87a7-f958de5951db",
   "metadata": {},
   "source": [
    "## Q1. 给定一个整数列表 [3, 5, 2, 7, 1]，找到其中的最大值和最小值，并打印两者之和。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c3e984df-1d1e-4b18-8826-d00ded94ac77",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "num_list = [3, 5, 2, 7, 1]\n",
    "max_num = num_list[0]\n",
    "min_num = num_list[0]\n",
    "\n",
    "for num in num_list:\n",
    "    if num > max_num:\n",
    "        max_num = num\n",
    "    elif num < min_num:\n",
    "        min_num = num\n",
    "\n",
    "print(max_num + min_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33adcfa1-25db-4ca7-8cad-8c99260cd49f",
   "metadata": {},
   "source": [
    "## Q2. 使用 while 循环输出 1 到 10 的所有奇数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1134678b-ce7f-458a-8ad6-e61e7283c668",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "num = 1\n",
    "while num <= 10:\n",
    "    if num % 2 != 0:\n",
    "        print(num)\n",
    "    num += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b42e3e69-fe15-4f57-871b-30acd986c03b",
   "metadata": {},
   "source": [
    "## Q3. 输入一个数字并将其转换为整数，如果输入的不是数字，则提示用户重新输入直到输入数字为止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "32ee243a-2a67-4103-a8ed-52b0adf4d098",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个数字： 1.5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入有误，请重新输入！\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个数字： 0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入的数字是： 0\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    try:\n",
    "        num = int(input(\"请输入一个数字：\"))\n",
    "        break\n",
    "    except ValueError:\n",
    "        print(\"输入有误，请重新输入！\")\n",
    "\n",
    "print(\"输入的整数是：\", num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3006095-014b-4328-96b7-0cd19445710b",
   "metadata": {},
   "source": [
    "## Q4. 求 100 以内的素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0e4ed68b-45c9-412b-856e-b0b9e54e8c90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n"
     ]
    }
   ],
   "source": [
    "prime_list = []\n",
    "\n",
    "for num in range(2, 101):\n",
    "    for i in range(2, num):\n",
    "        if num % i == 0:\n",
    "            break\n",
    "    else:\n",
    "        prime_list.append(num)\n",
    "\n",
    "print(prime_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb1204b2-1f67-47e5-8b6b-890e797d193d",
   "metadata": {},
   "source": [
    "## Q5. 请用至少两种不同办法计算1-100中奇数之和。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "51ab495d-f96b-4f57-807a-e0145fa142de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "for i in range(1, 101):\n",
    "    if i % 2 != 0:\n",
    "        sum_odd_numbers += i\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c4e4c8e5-43fc-4048-9f50-108e6be3d7c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "i = 1\n",
    "while i <= 100:\n",
    "    if i % 2 != 0:\n",
    "        sum_odd_numbers += i\n",
    "    i += 1\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "79fef2c4-fdde-47fd-903b-0f53a3888fe2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = sum([i for i in range(1, 101) if i % 2 != 0])\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "db2116fe-d215-4426-a7d7-0da6af46998d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = sum([i for i in range(1, 101, 2)])\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6e02c526-53db-4a74-a734-45dd03f0ad49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2500"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_odd_numbers = sum(list(range(1, 101, 2)))\n",
    "sum_odd_numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bf3223aa-f6e4-4c32-88d7-bb84314cc395",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "i = 1\n",
    "while i <= 100:\n",
    "    sum_odd_numbers += i\n",
    "    i += 2\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "11a6d0cb-8f4f-4af7-aba3-9d82df34da44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "numbers = list(range(1, 101))\n",
    "sum_odd_numbers = sum(numbers[::2])\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "437e7a05-3fb8-47df-9f11-7bca58b39ce0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "numbers = np.arange(1, 101)\n",
    "sum_odd_numbers = np.sum(numbers[numbers % 2 != 0])\n",
    "print(sum_odd_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "411cd03e-ddb2-46b8-a0fb-910483b8f2a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2500"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "count = 1\n",
    "while True:\n",
    "    sum_odd_numbers = sum_odd_numbers + count\n",
    "    count += 2\n",
    "    if count >= 100:\n",
    "        break\n",
    "\n",
    "sum_odd_numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9e84cbd5-366a-4644-8ffc-19409fac9506",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "result=0\n",
    "i=0\n",
    "while i<=100:\n",
    "    result+=(i % 2)*i\n",
    "    i+=1\n",
    "print(result)\n",
    "# 答案由 yeqihai 提供，谢谢。也请大家提供更多有趣逻辑。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2a76d332-aee9-4e69-b1d7-20b5eac340a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Repo: https://github.com/Visualize-ML\n",
    "# Book 1 Python for Beginners  |  From Basic Arithmetic to Machine Learning\n",
    "# Published and copyrighted by Tsinghua University Press\n",
    "# Beijing, China, 2023"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
